package ntut.andengine.game;

import java.util.List;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.DeathLight;
import ntut.andengine.physics.DeathLight.Direction;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel20 extends GameLevel {

	PhysicalComponent bar1;
    PhysicalComponent bar2;
    PhysicalComponent bar3;
    PhysicalComponent bar4;
    List<DeathLight> deathLight;
	GameLevel mLevel;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);
		
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {
		
        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(240, 720));
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel20.this, GameLevel10.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});
        
        PhysicalFactory.CreateWall(this, new Vector2(240, 780), 480, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(240, 20), 480, 40, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(20, 400), 40, 720, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(460, 400), 40, 720, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(80, 260), 80, 120, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(400, 260), 80, 120, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(80, 560), 80, 80, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(400, 560), 80, 80, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(240, 210), 240, 20, ComponentColor.Blue);
        PhysicalFactory.CreateWall(this, new Vector2(240, 310), 240, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(180, 380), 120, 20, ComponentColor.Red);
        PhysicalFactory.CreateWall(this, new Vector2(300, 380), 120, 20, ComponentColor.Blue);
        bar1 = PhysicalFactory.CreateBar(this, new Vector2(160, 380), ComponentColor.Blue, new Vector2(0, 20), false);
        bar2 = PhysicalFactory.CreateBar(this, new Vector2(320, 380), ComponentColor.Red, new Vector2(0, 20), false);
        bar3 = PhysicalFactory.CreateBar(this, new Vector2(160, 620), ComponentColor.Red, new Vector2(0, -20), false);
        bar4 = PhysicalFactory.CreateBar(this, new Vector2(320, 620), ComponentColor.Blue, new Vector2(0, -20), false);
        PhysicalFactory.CreateLaunchingPad(this, new Vector2(80, 420), true);
        PhysicalFactory.CreateDeathLight(this, new Vector2(180, 420), 120, 40, Direction.Right);
        PhysicalFactory.CreateLaunchingPad(this, new Vector2(400, 420), true);
        deathLight = PhysicalFactory.CreateDeathLight(this, new Vector2(300, 420), 120, 40, Direction.Left);
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(240, 120), ComponentColor.Blue);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);	
		mScene.registerUpdateHandler(new IUpdateHandler(){
			
			@Override
			public void onUpdate(float pSecondsElapsed) {
				
				CheckPutBack();
				redOrBlue();
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		});

		return mScene;
	}
	
	private void CheckPutBack() {
        
		if (bar1.getPosition().y > 620 || bar1.getPosition().y < 380){
            if(bar1.getPosition().y > 620 ){
            	bar1.setPosition(new Vector2(bar1.getPosition().x, 620));
            }
            if(bar1.getPosition().y < 380 ){
            	bar1.setPosition(new Vector2(bar1.getPosition().x, 380));
            }
        	bar1.setVelocity(bar1.getVelocity().mul(-1));
        }
        if (bar2.getPosition().y < 380 || bar2.getPosition().y > 620)
        {
        	if(bar2.getPosition().y > 620 ){
            	bar2.setPosition(new Vector2(bar2.getPosition().x, 620));
            }
            if(bar2.getPosition().y < 380 ){
            	bar2.setPosition(new Vector2(bar2.getPosition().x, 380));
            }
            bar2.setVelocity(bar2.getVelocity().mul(-1));
        }
        if (bar3.getPosition().y > 620 || bar3.getPosition().y < 380){
            if(bar3.getPosition().y > 620 ){
            	bar3.setPosition(new Vector2(bar3.getPosition().x, 620));
            }
            if(bar3.getPosition().y < 380 ){
            	bar3.setPosition(new Vector2(bar3.getPosition().x, 380));
            }
        	bar3.setVelocity(bar3.getVelocity().mul(-1));
        }
        if (bar4.getPosition().y < 380 || bar4.getPosition().y > 620)
        {
        	if(bar4.getPosition().y > 620 ){
            	bar4.setPosition(new Vector2(bar4.getPosition().x, 620));
            }
            if(bar4.getPosition().y < 380 ){
            	bar4.setPosition(new Vector2(bar4.getPosition().x, 380));
            }
            bar4.setVelocity(bar4.getVelocity().mul(-1));
        }        
    }
	
	private void redOrBlue(){
		
		if(mSparkle.getPosition().x>240 && mSparkle.getPosition().y>360){
			for(DeathLight deathLights : deathLight){
				deathLights.remove();
			}
		}
	}
}
